package com.west.imclient.view;

import imserverentity.entity.IChattable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

import java.awt.Font;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.border.LineBorder;

import com.west.imclient.RxBus;
import com.west.imclient.listener.SendActionListener;
import com.west.imclient.listener.ShakeActionListener;
import com.west.imclient.runnable.ReceiveRunnable;

public class MeetingRoomView extends JFrame {
	// public static String to = "West";

	private JLabel label;
	private JTextPane jtp;
	private JTextArea jta;
	private JButton sendbtn;
	private JButton shakebtn;
	private JList<String> cjl;

	private List<String> clients;
	private ObjectInputStream ois;
	private ObjectOutputStream oos;
	private IChattable client;

	public MeetingRoomView() {
		init();
	}

	public MeetingRoomView(ObjectInputStream ois, ObjectOutputStream oos,
			IChattable client) {
		operateBus();
		this.ois = ois;
		this.oos = oos;
		this.client = client;
		init();
	}

	public JLabel getLabel() {
		return label;
	}

	public JTextPane getJtp() {
		return jtp;
	}

	public JTextArea getJta() {
		return jta;
	}

	public JList getCjl() {
		return cjl;
	}

	public List<String> getClients() {
		return clients;
	}

	public void setClients(List<String> clients) {
		this.clients = clients;
	}

	public ObjectInputStream getOis() {
		return ois;
	}

	public void setOis(ObjectInputStream ois) {
		this.ois = ois;
	}

	public ObjectOutputStream getOos() {
		return oos;
	}

	public void setOos(ObjectOutputStream oos) {
		this.oos = oos;
	}

	public IChattable getClient() {
		return this.client;
	}

	public void init() {

		label = new JLabel("All");
		jtp = new JTextPane();
		jta = new JTextArea();
		sendbtn = new JButton("Send");
		shakebtn = new JButton("Shake");
		cjl = new JList<String>();

		add(label);
		add(jtp);
		add(jta);
		add(sendbtn);
		add(shakebtn);
		add(cjl);
		setTitle("IM Meeting Room");
		setSize(800, 600);
		setResizable(false);
		setLocationRelativeTo(null);
		setLayout(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		label.setBounds(0, 0, 800, 50);
		jtp.setBounds(0, 50, 600, 400);
		jtp.setFont(new Font("font1", 2, 12));
		jtp.setEnabled(false);
		jtp.setBorder(new LineBorder(new java.awt.Color(127, 157, 185), 3,
				false));
		jtp.setCaretPosition(jtp.getText().length());

		jta.setBounds(0, 450, 450, 100);
		jta.setBorder(new LineBorder(new java.awt.Color(127, 157, 185), 1,
				false));
		jtp.setCaretPosition(jta.getText().length());

		sendbtn.setBounds(450, 450, 150, 50);
		sendbtn.addActionListener(new SendActionListener(this, oos));

		shakebtn.setBounds(450, 500, 150, 50);
		shakebtn.addActionListener(new ShakeActionListener(this, oos));

		cjl.setBounds(650, 50, 100, 450);
		String[] list = { "All" };
		cjl.setListData(list);

		new Thread(new ReceiveRunnable(this, ois)).start();

	}

	private void operateBus() {
		RxBus.get().toFlowable().map(new Function<Object, String>() {
			@Override
			public String apply(@NonNull Object o) throws Exception {
				return (String) o;
			}

		}).subscribe(new Consumer<String>() {
			@Override
			public void accept(@NonNull String othersBean) throws Exception {
				if (othersBean != null) {
					System.out.println(othersBean);
					jtp.setText(jtp.getText() + "\n" + othersBean);
				}
			}

		});

	}
}
